/*
     Copyright 2007 Joseph M. Ferner

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

using System;
using System.IO;
using NMock2;
using NUnit.Framework;
using OpenServers.Net.Common.Utils;

namespace OpenServers.Net.Common.FileSystem.Http {
    [TestFixture]
    public class HttpFileTest : BaseNMockTest {
        private IDirectory _parent;
        private Url _url;
        private MockHttpFile _mockHttpFile;

        [SetUp]
        public override void SetUp() {
            base.SetUp();
            _parent = Mocks.NewMock<IDirectory>();
            Expect.AtLeast(0).On(_parent).GetProperty("FullPath").Will(Return.Value("/"));
            _url = new Url("http://localhost/test.txt");
            _mockHttpFile = new MockHttpFile(_parent, _url);
            _mockHttpFile._EnsureFileLoadedDelegate = delegate { };
            WriteToFile("hello world");
        }

        [Test]
        public void TestOpenInputStreamFileDoesNotExist() {
            _mockHttpFile.CacheFile.Delete();
            Assert.IsNull(_mockHttpFile.OpenInputStream());
            Assert.IsFalse(_mockHttpFile.Exists);
        }

        [Test]
        public void TestOpenInputStreamFileExists() {
            Stream input = _mockHttpFile.OpenInputStream();
            Assert.IsNotNull(input);
            string result = StreamUtils.ToString(input);
            Assert.AreEqual("hello world", result);
            Assert.IsTrue(_mockHttpFile.Exists);
        }

        [Test]
        [ExpectedException(typeof(NotImplementedException))]
        public void TestOpenOutputStream() {
            _mockHttpFile.OpenOutputStream();
        }

        [Test]
        public void TestSize() {
            Assert.AreEqual(11, _mockHttpFile.Size);
        }

        [Test]
        public void TestName() {
            Assert.AreEqual("test.txt", _mockHttpFile.Name);
        }

        [Test]
        public void TestLastModified() {
            DateTime dt = DateTime.Now;
            _mockHttpFile.CacheFile.LastWriteTime = dt;
            Assert.AreEqual(dt, _mockHttpFile.LastModified);
        }

        [Test]
        [ExpectedException(typeof(NotImplementedException))]
        public void TestDelete() {
            _mockHttpFile.Delete();
        }

        [Test]
        public void TestParent() {
            Assert.AreEqual(_parent, _mockHttpFile.Parent);
        }

        [Test]
        public void TestReadOnly() {
            Assert.IsTrue(_mockHttpFile.ReadOnly);
        }

        [Test]
        [ExpectedException(typeof(NotImplementedException))]
        public void TestMoveTo() {
            IFileSystemObject fso = Mocks.NewMock<IFileSystemObject>();
            _mockHttpFile.MoveTo(fso);
        }

        [Test]
        public void TestFullPath() {
            Assert.AreEqual("/test.txt", _mockHttpFile.FullPath);
        }

        [Test]
        public void TestUrl() {
            Assert.AreEqual("http://localhost/test.txt", _mockHttpFile.Url.ToString());
        }

        private void WriteToFile(string str) {
            StreamWriter writer = _mockHttpFile.CacheFile.CreateText();
            writer.Write(str);
            writer.Close();
        }

        private class MockHttpFile : HttpFile {
            public delegate void EnsureFileLoadedDelegate();

            public EnsureFileLoadedDelegate _EnsureFileLoadedDelegate;

            public MockHttpFile(IDirectory parent, Url url)
                : base(parent, url) {
            }

            protected override void EnsureFileLoaded() {
                if (_EnsureFileLoadedDelegate != null) {
                    _EnsureFileLoadedDelegate();
                }
            }
        }
    }
}
